package org.kernely.quartz;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;

import org.kernely.core.service.KernelyBundle;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.ui.IKernelyPresenter;
import org.kernely.ui.IKernelyView;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;

import com.vaadin.Application;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;

public class KernelyQuartzGui implements KernelyBundle {
	private static Table tableJobs = new Table();
	private static Dictionary panels;
	
	/**
	 * This method return an IndexedContainer of jobs
	 * @return
	 */
	private static IndexedContainer getQuartzJobsTable()
	{
        String[] fields = { "Job Name","Iteration","Frequence","Start Date","End Date"};
        ArrayList<JobDetail> jobs;
        IndexedContainer ic = new IndexedContainer();

        for (String p : fields) {
            ic.addContainerProperty(p, String.class, "");
        }
        CheckBox box = new CheckBox();
        box.setEnabled(false);
        ic.addContainerProperty("Activated", CheckBox.class,box);
        
		try {
			jobs = KernelyQuartzScheduler.getInstance().getScheduledJobs();
			for (int i = 0; i < jobs.size() ; ++i){
				Object id = ic.addItem();
				JobDetail jd = jobs.get(i);
				JobDataMap jobmap = jd.getJobDataMap();
				ic.getContainerProperty(id, "Job Name").setValue(jd.getName());
				ic.getContainerProperty(id, "Iteration").setValue(jobmap.getInt("count"));
				ic.getContainerProperty(id, "Frequence").setValue(jobmap.getInt("interval"));
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
        return ic;
		
	}
	
	/**
	 * Create and return a table's view of quartz jobs
	 * @return Panel
	 */
	public static Panel getQuartzPanel(){	
		Panel panel = new Panel();
		panel.setSizeFull();
		panel.addComponent(tableJobs);
		tableJobs.setSizeFull();
		tableJobs.setContainerDataSource(getQuartzJobsTable());
		tableJobs.setSelectable(true);
	    tableJobs.setImmediate(true);
		return panel;
	}
//	
//	public static void getQuartzMenu(Dictionary dico_panel,MenuBar menuBar){
//		panels = dico_panel;
//
//		MenuBar.Command quartzCommand = new MenuBar.Command() {
//			public void menuSelected(MenuItem selectedItem) {
//				((Panel)panels.get("EAST_PANEL")).removeAllComponents();
//				((Panel)panels.get("SOUTH_PANEL")).removeAllComponents();
//				((Panel)panels.get("NORTH_PANEL")).removeAllComponents();
//				((Panel)panels.get("EAST_PANEL")).addComponent(new Label("Detected Baches Configuration"));
//				((Panel)panels.get("EAST_PANEL")).addComponent(getQuartzPanel());
//			}
//		};
//		menuBar.addItem("Quartz",quartzCommand);
//		//return kernelyQuartzMenu;
//	}

	public void displayBundleSettings() {
		// TODO Auto-generated method stub
		
	}

	/*public MenuBar getMenu() {
		MenuBar menu = new MenuBar();
		menu.addItem("Jobs list", new MenuBar.Command() {			
			public void menuSelected(MenuItem selectedItem) {
				((Panel)panels.get("EAST_PANEL")).removeAllComponents();
				((Panel)panels.get("SOUTH_PANEL")).removeAllComponents();
				((Panel)panels.get("NORTH_PANEL")).removeAllComponents();
				((Panel)panels.get("EAST_PANEL")).addComponent(new Label("Detected Baches Configuration"));
				((Panel)panels.get("EAST_PANEL")).addComponent(getQuartzPanel());				
			}
		});
		return menu;
	}*/
	

	public String getName() {
		return "Kernely Quartz";
	}

	public void setKernelyPanels(Dictionary dicoPanel) {
		this.panels = dicoPanel;		
	}

	public void setKernelyMenuBar(MenuBar menu) {
		// TODO Auto-generated method stub
		
	}

	public void setModuleService(KernelyBundleService service) {
		System.out.println("KernelyQuartzBundle: registering with ModuleService");
		service.registerModule(this);
	}
	
	public void unsetModuleService(KernelyBundleService service) {
		System.out.println("KernelyQuartzBundle: unregistering with ModuleService");
		service.unregisterModule(this);
	}

	public boolean defaultParentMenuItem() {
		// TODO Auto-generated method stub
		return true;
	}

	public HashMap<String, Class<? extends IKernelyView>> getMenu() {
		// TODO Auto-generated method stub
		return null;
	}

	public HashMap<Class<? extends IKernelyView>, Class<? extends IKernelyPresenter>> getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	public Resource getResource(Application application) {
		// TODO Auto-generated method stub
		return null;
	}

}
